package zyn_hy_login;

public class CData
{
	public SqlDataAdapter sqladapter;
	public SqlCommandBuilder sqlbuilder;
	public SqlCommand sqlcmd;
	public SqlConnection sqlconn = new SqlConnection(DBCommon.strConnectstring);
	public SqlDataReader sqlreader;

	public final String creat_select_command(String[][] select_value, String table_name)
	{
		String text = "Select ";
		for (int i = 0; i < select_value[0].length; i++)
		{
			if (i == (select_value[0].length - 1))
			{
				text = text + select_value[0][i];
			}
			else
			{
				text = text + select_value[0][i] + ",";
			}
		}
		text = text + " From " + table_name;
		if (select_value[1].length != 0)
		{
			text = text + " Where ";
			for (int j = 0; j < select_value[1].length; j++)
			{
				if (j == (select_value[1].length - 1))
				{
					String text3 = text;
					text = text3 + select_value[1][j] + " = '" + select_value[2][j] + "'";
				}
				else
				{
					String text4 = text;
					text = text4 + select_value[1][j] + " = '" + select_value[2][j] + "', ";
				}
			}
		}
		return text;
	}

	public final void DataSet_fill(String sqltext, DataSet ds_table, String table_name)
	{
		this.sqladapter = new SqlDataAdapter(sqltext, this.sqlconn);
		this.sqladapter.Fill(ds_table, table_name);
	}

	public final void DataSet_update(String sqltext, DataSet ds_table, String table_name)
	{
		this.sqladapter = new SqlDataAdapter(sqltext, this.sqlconn);
		this.sqlbuilder = new SqlCommandBuilder(this.sqladapter);
		this.sqladapter.Update(ds_table, table_name);
	}

	public final String replace_string_yinhao(String sqlcmd)
	{
		return sqlcmd.replace("'", "''");
	}

	public final void SqlCommand_Execute(String sqltext)
	{
		this.sqlconn.Open();
		this.sqlcmd = new SqlCommand(sqltext, this.sqlconn);
		this.sqlcmd.ExecuteNonQuery();
		this.sqlconn.Close();
	}

	public final SqlDataReader SqlCommand_Execute_Return(String sqltext)
	{
		this.sqlconn.Open();
		this.sqlcmd = new SqlCommand(sqltext, this.sqlconn);
		this.sqlreader = this.sqlcmd.ExecuteReader();
		return this.sqlreader;
	}

	public final void SqlCommand_Insert(String[][] insert, String table_name)
	{
		this.sqlconn.Open();
		String cmdText = "Insert " + table_name + " (";
		for (int i = 0; i < insert[0].length; i++)
		{
			if (i == (insert[0].length - 1))
			{
				cmdText = cmdText + insert[0][i] + ") ";
			}
			else
			{
				cmdText = cmdText + insert[0][i] + ", ";
			}
		}
		cmdText = cmdText + "Values (";
		for (int j = 0; j < insert[0].length; j++)
		{
			if (j == (insert[1].length - 1))
			{
				cmdText = cmdText + "@" + insert[0][j] + ") ";
			}
			else
			{
				cmdText = cmdText + "@" + insert[0][j] + ", ";
			}
		}
		this.sqlcmd = new SqlCommand(cmdText, this.sqlconn);
		for (int k = 0; k < insert[0].length; k++)
		{
			String parameterName = "@" + insert[0][k];
			this.sqlcmd.Parameters.Add(parameterName, insert[1][k].toString());
		}
		this.sqlcmd.ExecuteNonQuery();
		this.sqlconn.Close();
	}

	public final void SqlCommand_Update(String[][] update_and_select, String table_name)
	{
		this.sqlconn.Open();
		String cmdText = "Update ";
		cmdText = cmdText + table_name + " set";
		for (int i = 0; i < update_and_select[0].length; i++)
		{
			if (i == (update_and_select[0].length - 1))
			{
				String text4 = cmdText;
				cmdText = text4 + " " + update_and_select[0][i] + " = @" + update_and_select[0][i] + "_update";
			}
			else
			{
				String text5 = cmdText;
				cmdText = text5 + " " + update_and_select[0][i] + " = @" + update_and_select[0][i] + "_update, ";
			}
		}
		cmdText = cmdText + " where";
		for (int j = 0; j < update_and_select[2].length; j++)
		{
			if (j == (update_and_select[2].length - 1))
			{
				String text6 = cmdText;
				cmdText = text6 + " " + update_and_select[2][j] + " = @" + update_and_select[2][j] + "_select";
			}
			else
			{
				String text7 = cmdText;
				cmdText = text7 + " " + update_and_select[2][j] + " = @" + update_and_select[2][j] + "_select, ";
			}
		}
		this.sqlcmd = new SqlCommand(cmdText, this.sqlconn);
		for (int k = 0; k < update_and_select[0].length; k++)
		{
			String parameterName = "@" + update_and_select[0][k] + "_update";
			this.sqlcmd.Parameters.Add(parameterName, update_and_select[1][k].toString());
		}
		for (int m = 0; m < update_and_select[2].length; m++)
		{
			String text3 = "@" + update_and_select[2][m] + "_select";
			this.sqlcmd.Parameters.Add(text3, update_and_select[3][m].toString());
		}
		this.sqlcmd.ExecuteNonQuery();
		this.sqlconn.Close();
	}
}